/*  1:   */ package nl.siegmann.epublib.bookprocessor;
/*  2:   */ 
/*  3:   */ import java.io.IOException;
/*  4:   */ import java.util.List;
/*  5:   */ import javax.xml.xpath.XPath;
/*  6:   */ import javax.xml.xpath.XPathExpressionException;
/*  7:   */ import javax.xml.xpath.XPathFactory;
/*  8:   */ import nl.siegmann.epublib.domain.Book;
/*  9:   */ import nl.siegmann.epublib.domain.Resource;
/* 10:   */ import nl.siegmann.epublib.domain.TOCReference;
/* 11:   */ import nl.siegmann.epublib.domain.TableOfContents;
/* 12:   */ import nl.siegmann.epublib.epub.BookProcessor;
/* 13:   */ import org.apache.commons.lang.StringUtils;
/* 14:   */ import org.xml.sax.InputSource;
/* 15:   */ 
/* 16:   */ public class SectionTitleBookProcessor
/* 17:   */   implements BookProcessor
/* 18:   */ {
/* 19:   */   public Book processBook(Book book)
/* 20:   */   {
/* 21:22 */     XPath xpath = createXPathExpression();
/* 22:23 */     processSections(book.getTableOfContents().getTocReferences(), book, xpath);
/* 23:24 */     return book;
/* 24:   */   }
/* 25:   */   
/* 26:   */   private void processSections(List<TOCReference> tocReferences, Book book, XPath xpath)
/* 27:   */   {
/* 28:28 */     for (TOCReference tocReference : tocReferences) {
/* 29:29 */       if (StringUtils.isBlank(tocReference.getTitle())) {
/* 30:   */         try
/* 31:   */         {
/* 32:33 */           String title = getTitle(tocReference, book, xpath);
/* 33:34 */           tocReference.setTitle(title);
/* 34:   */         }
/* 35:   */         catch (XPathExpressionException e)
/* 36:   */         {
/* 37:37 */           e.printStackTrace();
/* 38:   */         }
/* 39:   */         catch (IOException e)
/* 40:   */         {
/* 41:40 */           e.printStackTrace();
/* 42:   */         }
/* 43:   */       }
/* 44:   */     }
/* 45:   */   }
/* 46:   */   
/* 47:   */   private String getTitle(TOCReference tocReference, Book book, XPath xpath)
/* 48:   */     throws IOException, XPathExpressionException
/* 49:   */   {
/* 50:47 */     Resource resource = tocReference.getResource();
/* 51:48 */     if (resource == null) {
/* 52:49 */       return null;
/* 53:   */     }
/* 54:51 */     InputSource inputSource = new InputSource(resource.getInputStream());
/* 55:52 */     String title = xpath.evaluate("/html/head/title", inputSource);
/* 56:53 */     return title;
/* 57:   */   }
/* 58:   */   
/* 59:   */   private XPath createXPathExpression()
/* 60:   */   {
/* 61:58 */     return XPathFactory.newInstance().newXPath();
/* 62:   */   }
/* 63:   */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.bookprocessor.SectionTitleBookProcessor
 * JD-Core Version:    0.7.0.1
 */